#import "DongTaiUI.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "BasicInfoTableViewCell.h"
#import "DongTaiTableViewCell.h"
#import "XiuChangTableViewCell.h"
#import "TaGuanZhuTableViewCell.h"
#import "DengJiTableViewCell.h"
#import "ChatListViewController.h"
#import "TaGuanZhuDetailViewController.h"
#import "Tools.h"
#import "ConfigData.h"
#import "ASIFormDataRequest.h"
#import "JSON.h"
#import "UIColor+Hex.h"
@interface DongTaiUI ()

@end

@implementation DongTaiUI

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    dynamicArray=[[NSMutableArray alloc] initWithCapacity:0];
    
    
    _table.contentInset=UIEdgeInsetsMake(0, 0, 110, 0);
    _table.separatorStyle=NO;
    
    curSelectType = DongTaiSelectTypeDongTai;//初始化为动态
    
    NSLog(@"infoDic====%@",_infoDic);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 耗时的操作
        NSString*  dynamicURL=HOST;
        dynamicURL=[dynamicURL stringByAppendingString:@"index.php/AppUser/findUserDynamicPage"];
        NSLog(@"保存dynamicURL==%@",dynamicURL);
        NSURL* url=[[NSURL alloc] initWithString:dynamicURL];
        ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
        NSString*  uid=_infoDic[@"uid"];
        [req addPostValue:[NSString stringWithFormat:@"%@",uid] forKey:@"uid"];
        [req startSynchronous];
        NSError *error = [req error];
        if (!error) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [MBProgressHUD hideHUDForView:app.window animated:YES];
            
            NSString *response = [req responseString];
            NSLog(@"保存返回结果%@",response);
            NSDictionary* dic=[response JSONValue];
            NSArray*  list=dic[@"list"];
            if(list!=nil&&list.count>0){
                [dynamicArray addObjectsFromArray:list];
            }
            
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // 更新界面
            [_table reloadData];
        });
    });
    
}

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - table

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        return 210.0;//基本信息
    }
    else{
        if (curSelectType==DongTaiSelectTypeDongTai) {
            return 135;
        }
        else if (curSelectType==DongTaiSelectTypeXiuChang){
            return 90;
        }
        else if (curSelectType==DongTaiSelectTypeTaGuanZhu){
            
            return 50;
        }
        else if(curSelectType==DongTaiSelectTypeDengJi){
            
            return 190;
        }
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==0) {
        
        return 1;//基本信息
    }
    else{
        if (curSelectType==DongTaiSelectTypeDongTai) {
            return dynamicArray.count;
        }
        else if (curSelectType==DongTaiSelectTypeXiuChang){
            return 1;
        }
        else if (curSelectType==DongTaiSelectTypeTaGuanZhu){
            
            return 3;
        }
        else if(curSelectType==DongTaiSelectTypeDengJi){
            
            return 1;
        }
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        NSArray *unselectNameArr = @[@"动态",@"秀场",@"TA关注",@"等级"];
        NSArray *selectNameArr = @[@"动态红",@"秀场红",@"TA关注红",@"等级红"];
        
        
        NSString*  age=_infoDic[@"age"];
        BasicInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicInfoTableViewCell" forIndexPath:indexPath];
        // cell.headImageV = [UIImage imageNamed:@""]; 设置头像
        //cell.sexImageV.image = [UIImage imageNamed:@""];设置性别的图片
        cell.ageLab.text=[NSString stringWithFormat:@"%@",age];//年龄
        cell.xingzuoLab.text = @"处女座";//星座
        cell.xingzuoLab.font=[UIFont systemFontOfSize:10];
        
        cell.distanceLab.text = @"6.9km";//设置距离
        cell.distanceLab.font=[UIFont systemFontOfSize:10];
        //
        NSString*  last_login=_infoDic[@"last_login"];
        last_login=[last_login substringFromIndex:5];
        last_login=[last_login substringToIndex:11];
        NSLog(@"last_login==%@",last_login);
        cell.timeLab.text =[NSString stringWithFormat:@"%@",last_login]; //时间
        cell.timeLab.font=[UIFont systemFontOfSize:10];
        //动态按钮
        [cell.dongTaiBut addTarget:self action:@selector(clickOnBut:) forControlEvents:UIControlEventTouchUpInside];
        cell.dongTaiBut.tag = 0;
        
        //秀场按钮
        [cell.xiuChangBut addTarget:self action:@selector(clickOnBut:) forControlEvents:UIControlEventTouchUpInside];
        cell.xiuChangBut.tag = 1;
        
        //他关注按钮
        [cell.TaGuanZhuBut addTarget:self action:@selector(clickOnBut:) forControlEvents:UIControlEventTouchUpInside];
        cell.TaGuanZhuBut.tag = 2;
        
        //等级按钮
        [cell.dengJiBut addTarget:self action:@selector(clickOnBut:) forControlEvents:UIControlEventTouchUpInside];
        cell.dengJiBut.tag = 3;
        
        NSArray *butArr = @[cell.dongTaiBut,cell.xiuChangBut,cell.TaGuanZhuBut,cell.dengJiBut];
        for (UIButton *but in butArr) {
            [but setBackgroundImage:nil forState:0];
            if (but.tag==curSelectType) {
                [but setBackgroundImage:[UIImage imageNamed:selectNameArr[but.tag]] forState:0];
            }
            else{
                
                [but setBackgroundImage:[UIImage imageNamed:unselectNameArr[but.tag]] forState:0];
            }
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;//基本信息
    }
    else{
        if (curSelectType==DongTaiSelectTypeDongTai) {
            
            //动态的cell
            DongTaiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DongTaiTableViewCell" forIndexPath:indexPath];
            if(dynamicArray!=nil&&dynamicArray.count>0&&indexPath.row<dynamicArray.count){
                NSDictionary*  dic=[dynamicArray objectAtIndex:indexPath.row];
                NSLog(@"动态内容==%@",dic);
                
                if(dic){
                    NSString*  add_time=dic[@"add_time"];
                    add_time=[add_time substringFromIndex:5];
                    add_time=[add_time substringToIndex:11];
                    
                    //cell.headImabeView.image //设置头像
                    cell.timeLab.text =[NSString stringWithFormat:@"%@",add_time];
                    cell.zanCountLab.text = @"45";//赞的数量
                    cell.pinglunCountLab.text = @"58";//评论的数量
                    cell.desLab.text =dic[@"content"];
                }
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        else if (curSelectType==DongTaiSelectTypeXiuChang){
            
            //秀场的cell
            XiuChangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XiuChangTableViewCell" forIndexPath:indexPath];
            //cell.imageV  //就是秀场那张大图
            cell.countLab.text = @"451";//大图后面的数量
            //20,10,170,70
            //198,40,18,10
            //223,35,53,21
            if (indexPath.row%2==0) {
                [cell.imageV setFrame:CGRectMake(20,10,170,70)];
                [cell.XCGZImageV setFrame:CGRectMake(198,40,18,10)];
                [cell.countLab setFrame:CGRectMake(223,35,53,21)];
            }
            else{
                [cell.imageV setFrame:CGRectMake(320-20-170,10,170,70)];
                [cell.XCGZImageV setFrame:CGRectMake(320-20-170-53-18-10,40,18,10)];
                [cell.countLab setFrame:CGRectMake(320-20-170-53,35,53,21)];
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        else if (curSelectType==DongTaiSelectTypeTaGuanZhu){
            
            //他关注的cell
            TaGuanZhuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TaGuanZhuTableViewCell" forIndexPath:indexPath];
            if (indexPath.row==0) {
                cell.ImageV.image = [UIImage imageNamed:@"关注夜店"];
                cell.desLab.text = @"关注的夜店";
            }
            else if(indexPath.row==1){
                cell.ImageV.image = [UIImage imageNamed:@"粉丝"];
                cell.desLab.text = @"粉丝";
            }
            else if(indexPath.row==2){
                cell.ImageV.image = [UIImage imageNamed:@"收到的礼物"];
                cell.desLab.text = @"收到的礼物";
            }
            cell.tag = indexPath.row;
            
            UIImageView*  lineIv=[[UIImageView alloc] initWithFrame:CGRectMake(0,49.5, 320, 0.5)];
            lineIv.backgroundColor=[UIColor colorWithHexString:@"#e1e1e1"];
            [cell.contentView addSubview:lineIv];

            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        else if(curSelectType==DongTaiSelectTypeDengJi){
            
            //等级的cell
            DengJiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DengJiTableViewCell" forIndexPath:indexPath];
            cell.dengjiLab.text = @"LV.20";
            cell.rateLab.text = @"1670/2000";
            
            //黑色那条长度
            float blackWidth = cell.blackLab.frame.size.width;
            //根据比例来计算红色的那条的长度 这里假设的比例是 1820/2000
            float redWidth = blackWidth*1670/2000;
            CGRect redLabRect = cell.redLab.frame;
            redLabRect.size.width = redWidth;
            //重新设置红色条的长度
            cell.redLab.frame = redLabRect;
            cell.caiqiLab.text = @"45/5664";//财气指数
            cell.snsLab.text = @"455/4545"; //sns
            cell.darenLab.text = @"121/454";//达人指数
            cell.mingxingLab.text = @"4546/7487";//明星指数
            cell.reqiLab.text = @"784/1252";//人气指数
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        return nil;
    }
    
    
}

#pragma mark - 选择类型按钮

- (void)clickOnBut:(UIButton *)sender{
    
    curSelectType = sender.tag;
    [_table reloadData];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}


#pragma mark - Navigation


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   
    UIViewController *destinationVC = segue.destinationViewController;
    
    if ([sender isKindOfClass:[UIButton class]]) {
        
        if ([destinationVC respondsToSelector:@selector(setTitle:)]) {
            
            [destinationVC setValue:self.title forKey:@"title"];
        }
    }
    
    if ([sender isKindOfClass:[TaGuanZhuTableViewCell class]]) {
        
        TaGuanZhuTableViewCell *cell = sender;
        
        if ([(TaGuanZhuDetailViewController *)destinationVC respondsToSelector:@selector(setTaGuanZhuType:)]) {
            
            ((TaGuanZhuDetailViewController *)destinationVC).taGuanZhuType = cell.tag;
            if (cell.tag==0) {
                [destinationVC setValue:[NSString stringWithFormat:@"%@喜欢的夜店",self.title] forKey:@"title"];
            }
            else if(cell.tag==1){
                
                [destinationVC setValue:[NSString stringWithFormat:@"%@的粉丝",self.title] forKey:@"title"];
            }
            else{
                
                [destinationVC setValue:[NSString stringWithFormat:@"%@收到的礼物",self.title] forKey:@"title"];
            }
        }
    }
}

- (IBAction)attentionFn:(id)sender {
    NSUserDefaults*  ud=[[NSUserDefaults alloc] init];
    NSString*  mp=[ud objectForKey:@"mp"];
    if (mp&&mp.length>6) {
        NSString*  zhudong_uid=[ud objectForKey:@"uid"];
        zhudong_uid=[NSString stringWithFormat:@"%@",zhudong_uid];
        
        NSString*  beidong_uid=_infoDic[@"uid"];
        beidong_uid=[NSString stringWithFormat:@"%@",beidong_uid];
        
        
        NSString* loginURL=HOST;
        loginURL=[loginURL stringByAppendingString:@"index.php/appNearby/nearbyBottomGz"];
        NSURL* url=[[NSURL alloc] initWithString:loginURL];
        ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
      
        
        
        [req addPostValue:zhudong_uid forKey:@"zhudong_uid"];
        [req addPostValue:beidong_uid forKey:@"beidong_uid"];
        [req startSynchronous];
        NSError *error = [req error];
        if (!error) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [MBProgressHUD hideHUDForView:app.window animated:YES];
            NSString *response = [req responseString];
            NSLog(@"返回结果..%@",response);
            
            self.dic=[NSMutableDictionary dictionaryWithDictionary:[response JSONValue]];
            NSLog(@"登录失败..%@",self.dic);
            NSString* _code=[self.dic objectForKey:@"code"];
            int code=[_code intValue];
            if (code==1) {
                NSLog(@"关注成功......");
               [self.view makeToast:@"关注成功!" duration:1.0 position:@"center"];
            }
            if (code==7) {
                NSLog(@"您已经关注该用户,请不要重复关注......");
                [self.view makeToast:@"您已经关注该用户,请不要重复关注" duration:1.0 position:@"center"];
            }
            
        }

        
        
        
        
        //[self.view makeToast:@"关注成功!" duration:1.0 position:@"center"];
    }else{
        LoginViewController*  ui=[[LoginViewController alloc] init];
        [self.navigationController pushViewController:ui animated:NO];
    }
    
    
}

- (IBAction)chatFn:(id)sender {
        NSUserDefaults*  ud=[[NSUserDefaults alloc] init];
        NSString*  u=[ud objectForKey:@"mp"];
        if (u&&u.length>6) {
            NSLog(@"开始聊天......");
            NSString*  mp=_infoDic[@"mp"];
    
            ChatViewController* chatController = [[ChatViewController alloc] init];
    
    
            chatController.title = mp;
            //[conversation markMessagesAsRead:YES];
            //chatController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:chatController animated:YES];
        }else{
            LoginViewController*  ui=[[LoginViewController alloc] init];
            [self.navigationController pushViewController:ui animated:NO];
        }
    
    
    
    
    
    
    
}

- (IBAction)dazhaohuFn:(id)sender {
    [self.view makeToast:@"打招呼成功" duration:1.0 position:@"center"];
}

- (IBAction)giftFn:(id)sender {
    [self.view makeToast:@"送礼功能暂时不能使用,敬请期待下个版本发布!" duration:1.0 position:@"center"];
}
@end
